/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.aseclipse.core.runtime.registry {

import flash.events.Event;
import flash.utils.Dictionary;

import org.aseclipse.core.runtime.registry.classes.RegistryDelta;

/**
 * A registry change event implementation. A filter can be specified. In that case, only 
 * deltas for the selected hosts will be available to clients.
 */
public class RegistryChangeEvent extends Event {
	private var filter:String;
	private var deltas:Dictionary;
	
	public static const REGISTRY_CHANGE:String = "regsitryChanged";
	public function RegistryChangeEvent(deltas:Dictionary, filter:String) {
	    super(REGISTRY_CHANGE);
		this.deltas = deltas;
		this.filter = filter;
	}
	private function getHostDeltas():Array {
		// if there is a filter, return only the delta for the selected plug-in
		if (filter != null) {
			var singleDelta:RegistryDelta = getHostDelta(filter);
			return singleDelta == null ? new Array() : [singleDelta];
		}
		// there is no filter - return all deltas
		return toArray(deltas);
	}
	
	private function getHostDelta(pluginId:String):RegistryDelta {
		if (filter != null && pluginId != filter)
			return null;
		return deltas[pluginId] as RegistryDelta;
	}
	
	private function toArray(dic:Dictionary):Array {
        var array:Array = new Array();
        for (var key:Object in dic){
            array.push(dic[key]);
        }
        return array;
    }
	/** 
	 * Returns all extension deltas for the given host. Returns an empty array if there are
	 * no deltas in this event for any extension points provided by the given host. 
	 * 
	 * @param hostId the host identifier 
	 * @return all extension deltas for the given host 
	 */	
	public function getExtensionDeltas(hostName:String):Array {
	    if(hostName == null || hostName == "") {
	        var deltass:Array = getHostDeltas();
	        var total:Array = new Array();
	        for each(var item:RegistryDelta in deltass) {
	            var arry:Array = item.getAllExtensionDeltas();
	            for each(var one:Object in arry) {
	                total.push(one);
	            }
	        }
	    }
		var hostDelta:RegistryDelta = getHostDelta(hostName);
		if (hostDelta == null)
			return new IExtensionDelta[0];
		return hostDelta.getAllExtensionDeltas();
	}
	/** 
	 * Returns all the extension deltas for the given host and extension point. Returns an 
	 * empty array if there are no deltas in this event for the given extension point.
	 *  
	 * @param hostId the host identifier
	 * @param extensionPoint the simple identifier of the 
	 * extension point (e.g. <code>"builders"</code>)
	 * @return all extension deltas for the given extension point
	 */	
	public function getExtensionDeltasByPoint(hostName:String, extensionPoint:String):Array {
		var hostDelta:RegistryDelta = getHostDelta(hostName);
		if (hostDelta == null)
			return new IExtensionDelta[0];
		return hostDelta.getExtensionDeltas(hostName + '.' + extensionPoint);
	}
	/** 
	 * Returns the delta for the given host, extension point and extension. 
	 * Returns <code>null</code> if none exists in this event.
	 * 
	 * @param hostId the host identifier
	 * @param extensionPoint the simple identifier of the 
	 * extension point (e.g. <code>"builders"</code>)
	 * @param extension the unique identifier of the extension
	 * @return the extension delta, or <code>null</code>
	 */	
	public function getExtensionDelta(hostName:String, extensionPoint:String, extension:String):IExtensionDelta {
		var hostDelta:RegistryDelta = getHostDelta(hostName);
		if (hostDelta == null)
			return null;
		return hostDelta.getExtensionDelta(hostName + '.' + extensionPoint, extension);
	}
	
	override public function toString():String {
		return "RegistryChangeEvent:  " + getHostDeltas(); //$NON-NLS-1$
	}
}
}